package com.app.wechart.ChartManager;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.recyclerview.widget.RecyclerView;

import com.app.wechart.SocketUntitle.JWebSocketClient;
import com.app.wechart.SocketUntitle.MessageUntill;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;

public class MessageModule implements MessageUntill {


    private String userId;

    private static final MessageModule messageModule = new MessageModule();

    private Context context;

    private JWebSocketClient jWebSocketClient;

    private MessageListAdapter adapter;

    private Thread thread ;

    private Thread thread_sendmsg;

    private RecyclerView recyclerView;

    public static MessageModule getInstance(){
        return messageModule;
    }

    /**
     * 注册 开始建立连接
     *
     * @param userid 用户id
     */
    public MessageModule RegistWebSocket(String userid) {
        this.userId = userid;
        URI uri = URI.create("ws://172.20.10.2:8888/myHandler?userId=1");
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                jWebSocketClient = new JWebSocketClient(uri,messageModule);
                try {
                    jWebSocketClient.connectBlocking();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

        return messageModule;
    }

    public void loderMessage(Context c,MessageListAdapter adapter, RecyclerView recyclerView){
        this.context = c;
        this.adapter = adapter;
        this.recyclerView = recyclerView;
    }

    public void SendMsg(MessageInfo messageInfo) {
        try {
            if (messageInfo.type.equals("1")){
                this.addMsg(messageInfo);
                this.recyclerView.smoothScrollToPosition(this.adapter.arrayList.size());
            }
            if (jWebSocketClient.isOpen()==true){
                GsonBuilder gsonBuilder = new GsonBuilder();
                gsonBuilder.setPrettyPrinting();
                Gson gson = gsonBuilder.create();
                String  msg = gson.toJson(messageInfo);
                jWebSocketClient.send(msg);
            }
        }catch (Exception e){

        }

    }

    public void addMsg(MessageInfo messageInfo){
        this.adapter.addMessage(messageInfo);
        this.adapter.notifyDataSetChanged();
        this.recyclerView.smoothScrollToPosition(this.adapter.arrayList.size());
    }

    @Override
    public void onMessage(String msg) {
        Gson gson = new Gson();
        MessageInfo messageInfo =  gson.fromJson(msg,MessageInfo.class);
        messageInfo.fromType = "2";
        if (messageInfo.type.equals("1")){
            this.recyclerView.post(new Runnable() {
                @Override
                public void run() {
                    adapter.addMessage(messageInfo);
                  recyclerView.smoothScrollToPosition(adapter.arrayList.size());
                }
            });


        }

    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        // 重新连接
//        messageModule.RegistWebSocket(this.userId);
    }





    private Handler handler = new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.type = "2";
            messageInfo.msg = "connected";
            messageModule.SendMsg(messageInfo);
            handler.post(this);

        }
    };

    // 心跳包
    private void heartConnent(){
        handler.postDelayed(runnable,10000);
    }



}
